package com.lpc.carinfo.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.lpc.carinfo.bean.CarInfoBean;
import com.lpc.carinfo.util.UIUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lipc0113 on 2017/11/18.
 */

public class MyDB {

    private static MyDB myDB = null;
    private static SQLiteDatabase db;

    public static MyDB getInstace() {

        if (myDB == null) {
            myDB = new MyDB();
        }
        return myDB;
    }

    public static void init() {

        MyDbHelper myDbHelper = new MyDbHelper(UIUtil.getContext(), "carinfo", null, 1);

        db = myDbHelper.getWritableDatabase();
    }

    public boolean addOrUpdate(CarInfoBean bean) {

        try {
            if(isExit(bean.getCarSeries())){

                update(bean);
            }else{
                ContentValues values = new ContentValues();
                values.put(CarInfoDb.CARSERIES, bean.getCarSeries());
                values.put(CarInfoDb.CC, bean.getCc());
                values.put(CarInfoDb.ENGINEOIL, bean.getEngineOil());
                values.put(CarInfoDb.JILV, bean.getJiLv());
                values.put(CarInfoDb.KONGLV, bean.getKongLv());
                values.put(CarInfoDb.KONGTIAOLV, bean.getKongTiaoLv());
                values.put(CarInfoDb.QILV, bean.getQilv());
                values.put(CarInfoDb.HUOSAIHUA, bean.getHuosaihua());
                db.insert(CarInfoDb.CAR_INFO_TABLE, null, values);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public void update(CarInfoBean bean) {

        ContentValues values = new ContentValues();
        values.put(CarInfoDb.CARSERIES, bean.getCarSeries());
        values.put(CarInfoDb.CC, bean.getCc());
        values.put(CarInfoDb.ENGINEOIL, bean.getEngineOil());
        values.put(CarInfoDb.JILV, bean.getJiLv());
        values.put(CarInfoDb.KONGLV, bean.getKongLv());
        values.put(CarInfoDb.KONGTIAOLV, bean.getKongTiaoLv());
        values.put(CarInfoDb.QILV, bean.getQilv());
        values.put(CarInfoDb.HUOSAIHUA, bean.getHuosaihua());
        String whereClause = CarInfoDb.CARSERIES + " = ?";
        String[] whereArgs = {bean.getCarSeries()};
        db.update(CarInfoDb.CAR_INFO_TABLE, values, whereClause, whereArgs);

    }

    public boolean delete(String name) {

        try {
            String whereClause = CarInfoDb.CARSERIES + " = ?";
            String[] whereArgs = {name};
            db.delete(CarInfoDb.CAR_INFO_TABLE, whereClause,
                    whereArgs);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    //判断是否已经存在
    public boolean isExit(String name) {

        try {
            Cursor query = db.rawQuery("select * from " + CarInfoDb.CAR_INFO_TABLE
                    + " where " + CarInfoDb.CARSERIES + " = '" + name + "' order by " + CarInfoDb.CARSERIES, null);
            if(query.getCount() > 0){
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public List<CarInfoBean> find(String name) {

        List<CarInfoBean> lists = new ArrayList<>();
        Cursor query = null;

        String selection = CarInfoDb.CARSERIES + " = ?";
        String[] selectionArgs = {name};

        try {
            query = db.rawQuery("select * from " + CarInfoDb.CAR_INFO_TABLE
                    + " where " + CarInfoDb.CARSERIES + " like '%" + name + "%' order by " + CarInfoDb.CARSERIES, null);

            while (query.moveToNext()) {

                CarInfoBean bean = new CarInfoBean();
                bean.setCarSeries(query.getString(query.getColumnIndex(CarInfoDb.CARSERIES)));
                bean.setCc(query.getString(query.getColumnIndex(CarInfoDb.CC)));
                bean.setEngineOil(query.getString(query.getColumnIndex(CarInfoDb.ENGINEOIL)));
                bean.setJiLv(query.getString(query.getColumnIndex(CarInfoDb.JILV)));
                bean.setKongLv(query.getString(query.getColumnIndex(CarInfoDb.KONGLV)));
                bean.setKongTiaoLv(query.getString(query.getColumnIndex(CarInfoDb.KONGTIAOLV)));
                bean.setQilv(query.getString(query.getColumnIndex(CarInfoDb.QILV)));
                bean.setHuosaihua(query.getString(query.getColumnIndex(CarInfoDb.HUOSAIHUA)));

                lists.add(bean);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (query != null) {
                query.close();
            }
        }
        return lists;
    }

    public List<CarInfoBean> query() {

        List<CarInfoBean> lists = new ArrayList<>();
        Cursor query = null;
        try {
            query = db.query(CarInfoDb.CAR_INFO_TABLE, null, null
                    , null, null, null, CarInfoDb.CARSERIES, null);
            while (query.moveToNext()) {

                CarInfoBean bean = new CarInfoBean();
                bean.setCarSeries(query.getString(query.getColumnIndex(CarInfoDb.CARSERIES)));
                bean.setCc(query.getString(query.getColumnIndex(CarInfoDb.CC)));
                bean.setEngineOil(query.getString(query.getColumnIndex(CarInfoDb.ENGINEOIL)));
                bean.setJiLv(query.getString(query.getColumnIndex(CarInfoDb.JILV)));
                bean.setKongLv(query.getString(query.getColumnIndex(CarInfoDb.KONGLV)));
                bean.setKongTiaoLv(query.getString(query.getColumnIndex(CarInfoDb.KONGTIAOLV)));
                bean.setQilv(query.getString(query.getColumnIndex(CarInfoDb.QILV)));
                bean.setHuosaihua(query.getString(query.getColumnIndex(CarInfoDb.HUOSAIHUA)));

                lists.add(bean);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (query != null) {
                query.close();
            }
        }
        return lists;
    }
}
